import os

from my_json import download_json, save_json, bulid_download_path
from spider_url import bom_major_map_url

basic_path = str(os.path.dirname(__file__)) + "\\"
download_path = bulid_download_path(basic_path, True)


def init():
    url = bom_major_map_url()
    bom_major_map_1 = download_json(url.url_1, download_path + "bom_major_map_1.json", True)
    d = {}
    for bom_major_map_1_i in bom_major_map_1["msg"]:
        d[bom_major_map_1_i["key"]] = {"name": bom_major_map_1_i["name"]}
        bom_major_map0 = download_json(url.url0_to_string(bom_major_map_1_i["key"]),
                                       download_path + "bom_major_map0_" + bom_major_map_1_i["key"] + ".json", True)
        for bom_major_map0_i in bom_major_map0["msg"]:
            d[bom_major_map_1_i["key"]][bom_major_map0_i["key"]] = {"name": bom_major_map0_i["name"]}
            bom_major_map1 = download_json(url.url1_to_string(bom_major_map0_i["key"]),
                                           download_path + "bom_major_map1_" + bom_major_map0_i["key"] + ".json", True)
            for bom_major_map1_i in bom_major_map1["msg"]:
                d[bom_major_map_1_i["key"]][bom_major_map0_i["key"]][bom_major_map1_i["key"]] = {
                    "name": bom_major_map1_i["name"]}
                bom_major_map2 = download_json(url.url2_to_string(bom_major_map1_i["key"]),
                                               download_path + "bom_major_map2_" + bom_major_map1_i["key"] + ".json",
                                               True)
                for bom_major_map2_i in bom_major_map2["msg"]:
                    bom_major_map3 = download_json(url.url3_to_string(bom_major_map2_i["specId"]),
                                                   download_path + "bom_major_map3_" + bom_major_map2_i[
                                                       "zydm"] + ".json")
                    d[bom_major_map_1_i["key"]][bom_major_map0_i["key"]][bom_major_map1_i["key"]][
                        bom_major_map2_i["zydm"]] = bom_major_map3["msg"]
    save_json("bom_major_map.json", d)


init()
